import { defineStore } from "pinia";
import { store } from "@/store";
import { getNotice, addNotice } from "@/api/notice";
import { useUserStoreHook } from "@/store/modules/user";
import { message } from "@/utils/message";

export type NoticeType = {
  id: number;
  title: string;
  type: string;
  content: string;
  updateTime: string;
  status: number;
  imgs: Array<string>;
};

export type stateType = {
  noticeList: Array<NoticeType>;
  total: number;
};

export const useNoticeStore = defineStore({
  id: "pure-Notice",
  state: (): stateType => ({
    noticeList: [],
    total: 0
  }),
  actions: {
    async getNoticeInfo(currentPage: number, pigeSize: number) {
      await getNotice({
        id: useUserStoreHook().manageId,
        currentPage,
        pigeSize
      }).then(data => {
        this.total = data.total;
        this.noticeList = data.list;
      });
    },
    async addNoticeInfo(info: object) {
      await addNotice(info).then(() => {
        message("上传成功", { type: "success" });
      });
    }
  }
});

export function useNoticeStoreHook() {
  return useNoticeStore(store);
}
